package com.halflife.framework.participant.user;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.framework.participant.Department;
import com.halflife.framework.participant.DepartmentRepository;
import com.halflife.framework.participant.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class UserViewAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private UserViewRepository userViewRepository;
    @Resource
    private DepartmentRepository departmentRepository;
    @Resource
    private DepartmentService departmentService;

    @RequestMapping("/userView/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setPageSize(200);
        page.setQueryParam(queryParam);
        int totalCount = userViewRepository.selectCount(queryParam);
        List<UserView> list = userViewRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "userView/list";
    }

    @RequestMapping("/userView/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        List<Department> departments = departmentRepository.selectAll(new HashMap<>());
        model.addAttribute("departments", departments);
        UserView userView = new UserView();
        if(id!=null){
            userView = userViewRepository.selectByPrimaryKey(id);
        }
        model.addAttribute("userView",userView);
        return "userView/operate";
    }
    @RequestMapping("/userView/profile")
    public String profile(Integer id,String actionType,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        List<Department> departments = departmentRepository.selectAll(new HashMap<>());
        model.addAttribute("departments", departments);
        UserView userView = new UserView();
        if(id!=null){
            userView = userViewRepository.selectByPrimaryKey(id);
        }
        model.addAttribute("actionType",actionType);
        model.addAttribute("userView",userView);
        return "userView/user_profile";
    }

    @PostMapping("/userView/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(UserView userView, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append("select t.id from t_userview t where t.username='").append(userView.getUserName()).append("'");
            if(userView.getId()!=null){
                sb.append(" and t.id !=").append(userView.getId());

            }

            List list = userViewRepository.selectMany(sb.toString());
            if (list.size() > 0) {
                return new ResponseEntity("LoginName is already existed ;please change LoginName", HttpStatus.INTERNAL_SERVER_ERROR);
            }
            if(userView.getId()!=null){
                userViewRepository.updateByPrimaryKeySelective(userView);
            }else {
                userView.init();
                userView.setDepartmentId(3);
                userView.setType("3");
                Integer a = userViewRepository.insert(userView);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/userView/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            UserView userView = new UserView();
            userView.setId(id);
            userView.setValid(state==0?Boolean.FALSE:true);
            userViewRepository.updateByPrimaryKeySelective(userView);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
